class Utils {
    /**
     * 设置存储
     * @param {*} keyName
     * @param {*} keyValue
     */
    static setLocalItem(keyName, keyValue) {
        window.localStorage.setItem(keyName, keyValue);
    }
    /**
     * 获取存储
     * @param {*} keyName
     */
    static getLocalItem(keyName) {
        return window.localStorage.getItem(keyName);
    }

    /**
     * 保留小数点
     * @param {*} number 数字
     * @param {*} decimal 保留小数位数
     */
    static setFixed(number, decimal) {
        number = Number(number);
        return number.toFixed(decimal);
    }

    /**
     * 倒计时
     * @param {*} time
     */
    static doCountDown(time) {
        let timer = {
            expired: false, //是否过期
            day: 0, //剩余天数
            hour: 0, //剩余小时
            minute: 0, //剩余分
            second: 0, //剩余秒
            millsecond: 0 //毫秒
        };
        let countTime = time * 1000;
        let countDate = new Date(countTime).getTime();
        let nowDate = new Date().getTime();
        if (nowDate > countDate) {
            timer.expired = true;
            return timer;
        }
        let diff = countDate - nowDate;
        timer.day = Math.floor(diff / (1000 * 60 * 60 * 24));
        timer.hour = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        timer.minute = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
        timer.second = Math.floor((diff % (1000 * 60)) / 1000);
        timer.millsecond = Math.floor((diff % 1000) / 100);

        timer.day = this.setFixed4Time(timer.day);
        timer.hour = this.setFixed4Time(timer.hour);
        timer.minute = this.setFixed4Time(timer.minute);
        timer.second = this.setFixed4Time(timer.second);
        return timer;
    }

    /**
     * 时间保留2位数
     * @param {*} number
     */
    static setFixed4Time(number) {
        if (number < 10) {
            return "0" + number;
        }
        return number;
    }

    /**
     * 时间格式化
     * @param {*} time
     */
    static timeFormat(time) {
        return new Date(parseFloat(time + "000")).format("yyyy-MM-dd hh:mm:ss");
    }
    /**
     * 是否超时
     * @param {*} time
     */
    static timeOver(time) {
        let countTime = time * 1000;
        let countDate = new Date(countTime).getTime();
        let nowDate = new Date().getTime();
        return nowDate > countDate;
    }

    /**
     * 手机号格式化
     * @param {*} phone
     */
    static phoneFormat(phone) {
        phone = phone.replace(/[^\d]/g, "");
        let arr = phone.split("");
        let p = "";
        arr.forEach((v, i) => {
            if (i === 3 || i === 7) {
                p += " ";
            }
            p += v;
        });
        return p;
    }

    //获取路径参数
    static getQueryStr(name) {
        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        let r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    /**
     * 图片路径转换
     * @param {*} v
     * @param {*} picHost
     */
    static imageFilter(v, picHost) {
        if (
            v !== undefined &&
            v.substr(0, 4) !== "http" &&
            v.indexOf("static/img") == -1
        ) {
            return picHost + v;
        }
        return v;
    }
}
export default Utils;
